#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#include <pthread.h>
#include <sched.h>
#endif

typedef struct {
   int core_id;
   int load_percent;
} ThreadArgs;

// �߾���æ�ȴ������ߣ�΢�뼶��
void busy_sleep(int busy_percent) {
   const int total_time = 100000; // 100ms Ϊһ������
   int busy_time = total_time * busy_percent / 100;
   int sleep_time = total_time - busy_time;

#ifdef _WIN32
   LARGE_INTEGER freq, start, now;
   QueryPerformanceFrequency(&freq);
   QueryPerformanceCounter(&start);
   do {
       QueryPerformanceCounter(&now);
   } while ((now.QuadPart - start.QuadPart) * 1000000 / freq.QuadPart < busy_time);
   Sleep(sleep_time / 1000); // Windows ���ߵ�λΪ����
#else
   struct timespec start, now;
   clock_gettime(CLOCK_MONOTONIC, &start);
   long elapsed;
   do {
       clock_gettime(CLOCK_MONOTONIC, &now);
       elapsed = (now.tv_sec - start.tv_sec) * 1000000 + (now.tv_nsec - start.tv_nsec) / 1000;
   } while (elapsed < busy_time);
   usleep(sleep_time);
#endif
}

// �̺߳��������Ƶ������ĵĸ���
#ifdef _WIN32
DWORD WINAPI cpu_load_single(LPVOID args) {
#else
void* cpu_load_single(void* args) {
#endif
   ThreadArgs* params = (ThreadArgs*)args;

   // �󶨵�ָ������
#ifdef _WIN32
   SetThreadAffinityMask(GetCurrentThread(), 1ULL << params->core_id);
#else
   cpu_set_t cpuset;
   CPU_ZERO(&cpuset);
   CPU_SET(params->core_id, &cpuset);
   pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
#endif

   // ������������
   while (1) {
       busy_sleep(params->load_percent);
   }

   free(params);
   return NULL;
}

int main(int argc, char* argv[]) {
   if (argc != 3) {
       printf("Usage: %s <core> <load_percent>\n", argv[0]);
       return 1;
   }
   int k = atoi(argv[1]);
   int n = atoi(argv[2]);

   // ��ƽ̨��ȡ CPU ������
   int max_cores;
#ifdef _WIN32
   SYSTEM_INFO sysInfo;
   GetSystemInfo(&sysInfo);
   max_cores = sysInfo.dwNumberOfProcessors;
#else
   max_cores = sysconf(_SC_NPROCESSORS_ONLN);
#endif

   // �����Ϸ��Լ��
   if (k < 0 || k >= max_cores) {
       fprintf(stderr, "���󣺺��ı������ 0 �� %d ֮��\n", max_cores - 1);
       return 1;
   }
   if (n < 0 || n > 100) {
       fprintf(stderr, "���󣺸������� 0 �� 100 ֮��\n");
       return 1;
   }

   ThreadArgs* args = (ThreadArgs*)malloc(sizeof(ThreadArgs));
   if (!args) {
       fprintf(stderr, "�ڴ����ʧ��\n");
       return 1;
   }
   args->core_id = k;
   args->load_percent = n;

#ifdef _WIN32
   HANDLE thread = CreateThread(NULL, 0, cpu_load_single, args, 0, NULL);
   if (thread == NULL) {
       fprintf(stderr, "�̴߳���ʧ�� (������: %lu)\n", GetLastError());
       free(args);
       return 1;
   }
   WaitForSingleObject(thread, INFINITE);
   CloseHandle(thread);
#else
   pthread_t thread;
   if (pthread_create(&thread, NULL, cpu_load_single, args) != 0) {
       fprintf(stderr, "�̴߳���ʧ��\n");
       free(args);
       return 1;
   }
   pthread_join(thread, NULL);
#endif

   return 0;
}